#ifndef COLOR_CONVERT_H
#define COLOR_CONVERT_H

#include <QObject>

class color_convert : public QObject
{
    Q_OBJECT

    Q_PROPERTY(QString color_565 READ getColor_rgb565 WRITE setColor_rgb565 NOTIFY color_changed_rgb565)
    Q_PROPERTY(QString color_888 READ getColor_rgb888 WRITE setColor_rgb888 NOTIFY color_changed_rgb888)
    Q_PROPERTY(QString color_8888 READ getColor_argb8888 WRITE setColor_argb8888 NOTIFY color_changed_argb8888)
public:
    explicit color_convert(QObject *parent = nullptr);
    ~color_convert()
    {}

    void setColor_rgb565(QString color)
    {
        m_color_rgb565 = color;

        uint32_t rgb565 = color.toUInt(nullptr,16);

        uint32_t r = (rgb565 >> 11) & 0x01f;
        uint32_t g = (rgb565 >> 5) & 0x03f;
        uint32_t b = (rgb565 >> 0)& 0x01f;

        r = r << 3;
        g = g << 2;
        b = b << 3;

        uint32_t rgb888 = (r << 16) | (g << 8) | (b);
        m_color_rgb888 = QString::number(rgb888,16).toUpper();
        emit color_changed_rgb888(m_color_rgb888);

        uint32_t argb8888 = (0xff << 24) | (r << 16) | (g << 8) | (b);
        m_color_argb8888 = QString::number(argb8888,16).toUpper();
        emit color_changed_argb8888(m_color_argb8888);
    }
    QString getColor_rgb565(void)
    {
        return m_color_rgb565;
    }

    void setColor_rgb888(QString color)
    {
        m_color_rgb888 = color;

        uint32_t rgb888 = color.toUInt(nullptr,16);

        uint32_t r = (rgb888 >> 16) & 0x0ff;
        uint32_t g = (rgb888 >> 8) & 0x0ff;
        uint32_t b = (rgb888 >> 0) & 0x0ff;

        uint32_t rgb565 = ((r>>3) << 11) | ((g >> 2) << 5) | (b >> 3);
        m_color_rgb565 = QString::number(rgb565,16).toUpper();
        emit color_changed_rgb565(m_color_rgb565);

        uint32_t argb8888 = (0xff << 24) | (r << 16) | (g << 8) | (b);
        m_color_argb8888 = QString::number(argb8888,16).toUpper();
        emit color_changed_argb8888(m_color_argb8888);
    }
    QString getColor_rgb888(void)
    {
        return m_color_rgb888;
    }

    void setColor_argb8888(QString color)
    {
        m_color_argb8888 = color;

        uint32_t argb8888 = color.toUInt(nullptr,16);

        uint32_t r = (argb8888 >> 16)& 0x0ff ;
        uint32_t g = (argb8888 >> 8) & 0x0ff;
        uint32_t b = argb8888 & 0x0ff;

        uint32_t rgb565 = ((r>>3) << 11) | ((g >> 2) << 5) | (b >> 3);
        m_color_rgb565 = QString::number(rgb565,16).toUpper();
        emit color_changed_rgb565(m_color_rgb565);

        uint32_t rgb888 = (r << 16) | (g << 8) | (b);
        m_color_rgb888 = QString::number(rgb888,16).toUpper();
        emit color_changed_rgb888(m_color_rgb888);
    }

    QString getColor_argb8888(void)
    {
        return m_color_argb8888;
    }
signals:
    void color_changed_rgb565(const QString color);
    void color_changed_rgb888(const QString color);
    void color_changed_argb8888(const QString color);
private:
    QString m_color_rgb565;
    QString m_color_rgb888;
    QString m_color_argb8888;
};

#endif // COLOR_CONVERT_H
